package com.ygj.employeeReport.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.TicketCategories;
import com.base.common.enums.TicketStatus;
import com.base.common.persistent.model.Compounds;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.Ticket;
import com.cjy.common.bean.Files;
import com.cjy.common.bean.OpAttributes;
import com.ygj.employeeReport.service.complaint.ComplaintEmployeeService;

/**
 * 内部投诉Action
 * 
 * @author Kevin Colin
 * 
 */
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class   ComplaintEmployeeAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private ComplaintEmployeeService complaintEmployeeService;

	private int bid; // 设备标识符

	private String context; // 提交内容
	private int employeeId; // 员工id(主键ID)
	private String picUrls; // 图片地址,多地址以；分隔
	private String subUserPhone;// 提交人电话
	private int page; // 当前页数
	private String managerPhone; // 当前所属小区项目经理电话
	private int compoundsId; // 小区id

	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosFileName;
	private String[] filePhotosContentType;

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public File[] getFilePhotos() {
		return filePhotos;
	}

	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}

	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}

	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}

	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}

	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public String getManagerPhone() {
		return managerPhone;
	}

	public void setManagerPhone(String managerPhone) {
		this.managerPhone = managerPhone;
	}

	public String getSubUserPhone() {
		return subUserPhone;
	}

	public void setSubUserPhone(String subUserPhone) {
		this.subUserPhone = subUserPhone;
	}

	public String getPicUrls() {
		return picUrls;
	}

	public void setPicUrls(String picUrls) {
		this.picUrls = picUrls;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	/**
	 * 提交投诉信息
	 * 
	 * @return
	 */
	@Action(value = "subComplaintMessByEmployee", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String subComplaintMessByEmployee() {
		try {
			Ticket ticket = new Ticket();
			ticket.setContext(context);
			ticket.setPicUrls(picUrls);
			ticket.setStatus(TicketStatus.DISPATCH.getValue());
			ticket.setSubUserPhone(subUserPhone);
			ticket.setEmployee(new Employee().setId(employeeId));
			ticket.setCompounds(new Compounds().setId(compoundsId));
			ticket.setCategories(TicketCategories.EMPLOYEECOMLAINT.getValue());

			Files files = new Files();
			files.setFile(filePhotos);
			files.setFileContentType(filePhotosContentType);
			files.setFileFileName(filePhotosFileName);
			// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
			HttpServletRequest servletRequest = new StrutsRequestWrapper(
					request);
			String basePath = null;
			String path = servletRequest.getContextPath();
			basePath = servletRequest.getScheme() + "://"
					+ ConfigConstants.SERVERIP + ":"
					+ servletRequest.getServerPort() + path + "/";
			boolean disflag = false;
			String result = complaintEmployeeService.subTickets(ticket, disflag, files,
					basePath, bid);
			if (result == null) {
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 0));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 1).add("result",
								new OpAttributes().add("ticketCode", result)));
			}
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}

		return returnType();
	}

	/**
	 * 获取投诉信息列表根据员工和当前页数
	 * 
	 * @return
	 */
	@Action(value = "getComplaintEmployeeTickets")
	public String getTickets() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add(
					"result",
					complaintEmployeeService
							.getComplaintEmployeeTicketsByEmployeeId(
									((List<Employee>) request.getSession()
											.getAttribute("users")).get(0)
											.getId(), page)));
		} catch (Exception exception) {
			exception.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 根据工单类型和员工id获取工单总条数
	 * 
	 * @return
	 */
	@Action("getComplaintEmployeeTicketCounts")
	public String getComplaintEmployeeTicketCounts() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add(
					"result",
					new OpAttributes().add("totalCounts",
							complaintEmployeeService.getTicketCounts(
									((List<Employee>) request.getSession()
											.getAttribute("users")).get(0)
											.getId(),
									TicketCategories.EMPLOYEECOMLAINT
											.getValue()))));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

}
